package com.spark.lola.service.impl;

import com.spark.lola.entity.statistics.StatisticsStoreProduct;
import com.spark.lola.entity.statistics.StatisticsSupplier;
import com.spark.lola.entity.warehouse.*;
import com.spark.lola.mapper.*;
import com.spark.lola.service.IStatisticsService;
import com.spark.lola.service.IStoreInfoService;
import com.spark.lola.support.common.constant.BooleanConstant;
import com.spark.lola.support.common.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 采集日志 服务实现类
 * </p>
 * @author dengyejun
 * @since 2020-06-10
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private StoreInfoMapper storeInfoMapper;
    @Autowired
    private StoreSupplierMapper storeSupplierMapper;
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private StoreProductMapper storeProductMapper;

    @Autowired
    private StatisticsStoreProductMapper statisticsStoreProductMapper;
    @Autowired
    private StatisticsSupplierMapper statisticsSupplierMapper;

    @Override
    public void calStore(OrderInfo orderInfo) throws Exception {
        // 更新仓库统计
        StoreInfo storeInfo = storeInfoMapper.selectById(orderInfo.getStoreId());
        if (orderInfo.getStatus().equals(BooleanConstant.TRUE)) {
            storeInfo.setOrderPayment(storeInfo.getOrderPayment().add(orderInfo.getTotal()));
        } else {
            storeInfo.setOrderCount(storeInfo.getOrderCount() + 1);
            storeInfo.setOrderWeight(storeInfo.getOrderWeight().add(orderInfo.getWeight()));
            storeInfo.setOrderSales(storeInfo.getOrderSales().add(orderInfo.getTotal()));
        }
        storeInfoMapper.updateById(storeInfo);

    }


    @Override
    public void calProduct(OrderInfo orderInfo) throws Exception {
        ProductInfo productInfo = productInfoMapper.selectByNo(orderInfo.getProductNo());
        if (productInfo == null) {
            productInfo = new ProductInfo(orderInfo.getProductNo(), orderInfo.getProductName());
        }
        productInfo.setOrderCount(productInfo.getOrderCount() + 1);
        productInfo.setOrderWeight(productInfo.getOrderWeight().add(orderInfo.getWeight()));
        productInfo.setOrderSales(productInfo.getOrderSales().add(orderInfo.getTotal()));
        if (productInfo.getId() == null) {
            productInfoMapper.insert(productInfo);
        } else {
            productInfoMapper.updateById(productInfo);
        }
    }


    @Override
    public StatisticsStoreProduct calStoreProduct(OrderInfo orderInfo) throws Exception {
        StoreProduct model = storeProductMapper.selectByStoreAndNo(orderInfo.getStoreId(), orderInfo.getProductNo());
        if (model == null) {
            throw new Exception("货品类目【" + orderInfo.getStoreId() + "-" + orderInfo.getProductNo() + "-" + orderInfo.getProductName() + "】不存在!");
        }

        model.setOrderCount(model.getOrderCount() + 1);
        model.setOrderWeight(model.getOrderWeight().add(orderInfo.getWeight()));
        model.setOrderSales(model.getOrderSales().add(orderInfo.getTotal()));
        storeProductMapper.updateById(model);

        logger.info("===处理仓库货品【{}】日报【{}】订单【{}】===START===", orderInfo.getProductNo(), orderInfo.getTradeDay(), orderInfo.getNo());
        StatisticsStoreProduct statisticsStoreProduct = statisticsStoreProductMapper.findOne(orderInfo.getStoreId(),orderInfo.getProductNo(), TimeUtil.getDate(orderInfo.getTradeDay()));
        if (statisticsStoreProduct == null) {
            statisticsStoreProduct = new StatisticsStoreProduct(orderInfo);
        }
        statisticsStoreProduct.setOrderCount(statisticsStoreProduct.getOrderCount() + 1);
        statisticsStoreProduct.setOrderSales(statisticsStoreProduct.getOrderSales().add(orderInfo.getTotal()));
        statisticsStoreProduct.setOrderWeight(statisticsStoreProduct.getOrderWeight().add(orderInfo.getWeight()));
        if (statisticsStoreProduct.getId() == null) {
            statisticsStoreProductMapper.insert(statisticsStoreProduct);
        } else {
            statisticsStoreProductMapper.updateById(statisticsStoreProduct);
        }
        logger.info(statisticsStoreProduct.toString());
        logger.info("===处理仓库货品【{}】日报【{}】订单【{}】===END===", orderInfo.getProductNo(), orderInfo.getTradeDay(), orderInfo.getNo());
        return statisticsStoreProduct;
    }

    @Override
    public void calSupplier(OrderInfo orderInfo) throws Exception {
        StoreSupplier storeSupplier = storeSupplierMapper.selectByNo(orderInfo.getStoreId(),orderInfo.getSupplierNo());
        if (storeSupplier == null) {
            throw new Exception("供应商不存在!");
        }
        if (orderInfo.getStatus().equals(BooleanConstant.TRUE)) {
            // 订单结算
            storeSupplier.setOrderPayment(storeSupplier.getOrderPayment().add(orderInfo.getTotal()));
        } else {
            storeSupplier.setOrderCount(storeSupplier.getOrderCount() + 1);
            storeSupplier.setOrderWeight(storeSupplier.getOrderWeight().add(orderInfo.getWeight()));
            storeSupplier.setOrderSales(storeSupplier.getOrderSales().add(orderInfo.getTotal()));
        }
        storeSupplierMapper.updateById(storeSupplier);

        logger.info("===处理供应商【{}】日报【{}】订单【{}】===START===", orderInfo.getProductNo(), orderInfo.getTradeDay(), orderInfo.getNo());
        StatisticsSupplier statisticsSupplier = statisticsSupplierMapper.findOne(storeSupplier.getId(), TimeUtil.getDate(orderInfo.getTradeDay()));
        if (statisticsSupplier == null) {
            statisticsSupplier = new StatisticsSupplier(storeSupplier.getId(),orderInfo);
        }
        if (orderInfo.getStatus().equals(BooleanConstant.TRUE)) {
            // 订单结算
            statisticsSupplier.setOrderPayment(statisticsSupplier.getOrderPayment().add(orderInfo.getTotal()));
        }else {
            statisticsSupplier.setOrderCount(statisticsSupplier.getOrderCount() + 1);
            statisticsSupplier.setOrderSales(statisticsSupplier.getOrderSales().add(orderInfo.getTotal()));
            statisticsSupplier.setOrderWeight(statisticsSupplier.getOrderWeight().add(orderInfo.getWeight()));
        }
        if (statisticsSupplier.getId() == null) {
            statisticsSupplierMapper.insert(statisticsSupplier);
        } else {
            statisticsSupplierMapper.updateById(statisticsSupplier);
        }
        logger.info(statisticsSupplier.toString());
        logger.info("===处理供应商【{}】日报【{}】订单【{}】===END===", orderInfo.getProductNo(), orderInfo.getTradeDay(), orderInfo.getNo());
    }
}
